package com.queming.service.playlist;

import com.queming.music.dto.PlayListDto;
import com.queming.music.dto.PlayListSearchFilter;
import com.queming.music.entity.Playlist;
import com.queming.core.exception.ExceptionType;
import com.queming.core.mapper.MapperInterface;
import com.queming.music.mapper.PlayListMapper;
import com.queming.music.repository.PlayListRepository;
import com.queming.music.repository.specs.PlayListSpecification;
import com.queming.core.respository.specs.SearchCriteria;
import com.queming.core.respository.specs.SearchOperation;
import com.queming.service.artist.TraceableGeneralServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

/**
 * @author chencidiao
 * @date 2022/05/05 21:53
 **/
@Service
public class PlayListServiceImpl extends TraceableGeneralServiceImpl<Playlist,PlayListDto> implements PlayListService{

    private PlayListRepository repository;
    @Autowired
    public void setRepository(PlayListRepository repository) {
        this.repository = repository;
    }

    private PlayListMapper mapper;
    @Autowired
    public void setMapper(PlayListMapper mapper) {
        this.mapper = mapper;
    }


    @Override
    public Page<PlayListDto> search(PlayListSearchFilter playListSearchFilter) {
        PlayListSpecification specs = new PlayListSpecification();
        specs.add(new SearchCriteria("name",playListSearchFilter.getName(), SearchOperation.MATCH));
        if (playListSearchFilter.getRecommended()!=null){
            specs.add(new SearchCriteria("recommended",playListSearchFilter.getRecommended(),SearchOperation.EQUAL));
        }
        return repository.findAll(specs,playListSearchFilter.toPageable()).map(mapper::toDto);
    }


    @Override
    public PlayListDto recommend(String id, Integer recommendedFactor) {
        Playlist entity = getEntity(id);
        entity.setRecommended(true);
        entity.setRecommendedFactor(recommendedFactor);
        return mapper.toDto(repository.save(entity));
    }

    @Override
    public PlayListDto cancelRecommendation(String id) {
        Playlist entity = getEntity(id);
        entity.setRecommended(false);
        entity.setRecommendedFactor(0);
        return mapper.toDto(repository.save(entity));
    }

    @Override
    public PlayListDto makeSpecial(String id) {
        Playlist entity = getEntity(id);
        entity.setSpecial(true);
        return mapper.toDto(repository.save(entity));
    }

    @Override
    public PlayListDto cancelSpecial(String id) {
        Playlist entity = getEntity(id);
        entity.setSpecial(false);
        return mapper.toDto(repository.save(entity));
    }

    @Override
    public JpaRepository<Playlist, String> getRepository() {
        return repository;
    }

    @Override
    public MapperInterface<Playlist, PlayListDto> getMapper() {
        return mapper;
    }

    @Override
    public ExceptionType getNotFoundExceptionType() {
        return ExceptionType.PLAYLIST_NOT_FOUND;
    }
}
